package com.cheer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.MallAdminDao;
import com.cheer.dto.MallAdminRegisterDTO;
import com.cheer.dto.PasswordUpdateDTO;
import com.cheer.entity.MallAdminEntity;
import com.cheer.enums.AdminRoleEnum;
import com.cheer.exception.ServiceException;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.MallAdminService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("mallAdminService")
public class MallAdminServiceImpl extends ServiceImpl<MallAdminDao, MallAdminEntity> implements MallAdminService {

    @Autowired
    private MallAdminDao mallAdminDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Override
    public void addShopAdmin(String phone) {
        MallAdminEntity admin = new MallAdminEntity()
                .setUsername(phone)
                .setPassword(passwordEncoder.encode("000000"))
                .setRole(AdminRoleEnum.SHOP.getCode());

        mallAdminDao.insert(admin);
    }

    @Override
    public void resetShopPassword(String username) {
        MallAdminEntity adminEntity = new MallAdminEntity()
                .setPassword(passwordEncoder.encode("000000"));

        mallAdminDao.updateByUsername(username, adminEntity);
    }

    @Override
    public void updateUsername(String currentUsername, String targetUsername) {
        MallAdminEntity admin = new MallAdminEntity()
                .setUsername(targetUsername);

        mallAdminDao.updateByUsername(currentUsername, admin);
    }

    @Override
    public MallAdminEntity getByUsername(String username) {
        return mallAdminDao.selectOneByUsername(username);
    }

    @Override
    public void deleteByUsername(List<String> usernameList) {
        if (CollectionUtils.isEmpty(usernameList)) {
            return;
        }

        mallAdminDao.deleteByUsername(usernameList);
    }

    @Override
    public void add(MallAdminRegisterDTO adminRegisterDTO) {
        MallAdminEntity admin = new MallAdminEntity()
                .setUsername(adminRegisterDTO.getUsername())
                .setPassword(passwordEncoder.encode(adminRegisterDTO.getPassword()))
                .setRole(adminRegisterDTO.getRole());

        mallAdminDao.insert(admin);
    }

    @Override
    public void updatePassword(PasswordUpdateDTO passwordUpdateDTO) {
        String username = userInfoHolder.getAdmin().getUsername();
        MallAdminEntity admin = mallAdminDao.selectOneByUsername(username);
        if (admin == null) {
            throw new ServiceException("帐号不存在");
        }

        if (!passwordEncoder.matches(passwordUpdateDTO.getOldPassword(), admin.getPassword())) {
            throw new ServiceException("旧密码输入错误");
        }

        MallAdminEntity updateAdmin = new MallAdminEntity()
                .setId(admin.getId())
                .setPassword(passwordEncoder.encode(passwordUpdateDTO.getNewPassword()));

        mallAdminDao.updateById(updateAdmin);
    }

}